# simply utilize hash function to do transformation.
# may not grarantee uniqueness?
import hashlib

def md5(string):
    if type(string) == str:
        string = string.encode("utf-8")
    return hashlib.md5(string).hexdigest()

def det_hash(string):
    target = md5(string)
    #print(target)
    numbers = [int("0x"+x,16) for x in target]
    #print(numbers)
    roll = sum(numbers)
    roll = roll%5 +7
    target = target[:roll]
    return target

def ndet_hash(string,n=2):
    assert type(n) == int and n>1
    target = string
    for _ in range(n):
        target = md5(target)
    #print(target)
    numbers = [int("0x"+x,16) for x in target]
    #print(numbers)
    roll = sum(numbers)
    roll = roll%5 +7
    target = target[:roll]
    return target

def double_hash(string0,string1):
    first = det_hash(string)
    second = first + string1
    return det_hash(second)

def ndouble_hash(string0,string1,n=2):
    first = ndet_hash(string,n=n)
    second = first + string1
    return ndet_hash(second,n=n)

if __name__ == "__main__":
    string = "hello world"
    target = md5(string)
    #print(target)
    numbers = [int("0x"+x,16) for x in target]
    #print(numbers)
    roll = sum(numbers)
    roll = roll%5 +7
    target = target[:roll]
    print(target)
#    target = md5(string)
#    print(target.digest())
#    print(type(target.hexdigest()))
